<template>
  <view v-if="xShow==true">
		<view class="w100 bg-white text-center text-gray u-flex u-row-center">
			<scroll-view scroll-x= "true" scroll-left="0" scroll-with-animation="true" >
				<view class="u-flex u-row-around" style="height: 100upx;">
					<view v-for="(itme,index) in oneMenu" :key="index">
						<view class="u-p-20" :class="xid === itme._id?'active':''" style="" @click="yemx(index)">{{itme.name}}</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<kd-preview-image :images='preview_imgs' v-model="preview" :current="preview_index"></kd-preview-image> 
		<view class=" u-p-l-30 u-p-r-30">
			<view class=" bg-white margin-top radius" v-for="(xitem,xindex) in yemxList" :key="xindex">	
				<view class="">
					<view class="u-p-20 bg-white u-m-t-30">
						<view class=" u-flex " style="color: #393D4A;margin-bottom: 15px;">
							<u-tag v-if="xitem.first_trial.id==1" :text="xitem.first_trial.str" type="warning" mode="dark" shape="circle" size='mini'/>
							<u-tag v-if="xitem.first_trial.id==2" :text="xitem.first_trial.str" type="info" mode="dark" shape="circle" size='mini'/>
							<u-tag v-if="xitem.first_trial.id==0" :text="xitem.first_trial.str" type="primary" mode="dark" shape="circle" size='mini'/>
							<u-tag v-if="xitem.first_trial.id==3" :text="xitem.first_trial.str" type="error" mode="dark" shape="circle" size='mini'/>
							<u-tag v-if="xitem.first_trial.id==8" :text="xitem.first_trial.str" type="info" mode="dark" shape="circle" size='mini'/>
							<view style="font-size: 12pt;font-weight: bold;padding-left: 5px;"> {{xitem.realname}}</view>
						</view>
						<view style="margin: 10px 0">身份证号码：{{xitem.idcard}}</view>
						<view class="u-flex u-col-top">身份证照片：
							<image @click="previewfn([xitem.idcard_imgurl],0)" :src="xitem.idcard_imgurl" style="width: 100upx;height: 100upx;margin-right: 10upx;border-radius: 10upx;"></image>
						</view>	
						<view>征信报告：{{xitem.entity_report==1?'已提供纸质报告':''}}</view>
					 	<view class=" u-flex u-p-20 radius" style="width:100%;background: #FAFAFA;" v-if="xitem.report">
							<!-- <view class="" style="font-size: 10pt; width: 400upx; color: #393D4A;">{{sitme.goods_title}}</view> -->
							<image class=" radius u-m-r-10" @click="previewfn(xitem.report,sindex)" v-for="(sitme,sindex) in xitem.report" :key="sindex" v-if="sindex<4" :src="sitme" style="width: 100upx;height: 100upx;"></image>
							<view @click="previewfn(xitem.report,0)" class="" v-if="xitem.report.length>4">...</view>
							<view @click="previewfn(xitem.report,0)" class=" text-right" v-if="xitem.report.length>4" style="font-size: 10pt; width: 100upx; color: #393D4A; padding-top:5upx;">共{{xitem.report.length}}张</view>
						</view>
						<view v-if="xitem.remark">备注：{{xitem.remark}}</view>
						<view class="u-flex u-row-between u-m-t-10"> 
							<uni-dateformat :date="xitem.inTime*1000" :format="xitem.year==year?'M月d日 h:mm':'yyyy月M月d日 h:mm'"></uni-dateformat>
							<view class="u-flex">
								
								<u-button type="" size="mini" v-if='xitem.first_trial.id==8' @click="delOrder(xindex)">
									删除案件
								</u-button>
								<!-- <u-button class="u-m-l-10" type="error" size="mini" @click="addShouli(xindex)" v-if="xitem.state.id==0&&xitem.first_trial.id==1">
									受理
								</u-button> -->
								<u-button class="u-m-l-10" type="" size="mini" @click="qxOrder(xindex)" v-if="xitem.state.id==0&&xitem.first_trial.id==0">
									取消案件
								</u-button>
							</view>
						</view>
					</view>
				</view>
			</view>
			<u-empty class="u-p-30" text="暂无案件" mode="list" v-if="yemxList.length==0 && status != 'loading'"></u-empty>
			<u-loadmore class="u-p-30" :status="status" :load-text="{
				loadmore: '轻轻上拉',
				loading: '努力加载中',
				nomore: '我也是有底线的'
			}" v-else=""/>
		</view>
  </view>
</template>
<script>
let timestamp = Date.parse(new Date());
let date = new Date(timestamp);
	
import uniDateformat from 'components/uni-dateformat/uni-dateformat.vue';
import ksjPreviewImage from 'components/kd-preview-image/kd-preview-image.vue';
	export default {
		components: {
			uniDateformat,ksjPreviewImage
		},
		//#ifdef MP-WEIXIN
		onShareAppMessage(res) {//分享
			try {//从本地缓存中同步获取指定 key 对应的内容。
				var mv = uni.getStorageSync('userinfo');
				if (res.from === 'button') {// 来自页面内分享按钮
				 console.log(res.target)
				}
				// return {
				//  title: xv.store.stores_name,
				//  path: '/pages/home/index?member_openid='+mv.openid+"&stores_id="+xv.store.stores_id
				// }
			} catch (e) {
				// error
			}
			
		 },
		//#endif
		data() {
			return {
				xShow:false,
				preview:false,
				preview_imgs:[],
				preview_index:0,
				xid:'',
				oneMenu:[
					{
						'_id':'',
						'name':'全部',
					},
					{
						'_id':0,
						'name':'初审中',
					},
					{
						'_id':3,
						'name':'待回访',
					},
					{
						'_id':1,
						'name':'可受理',
					},
					{
						'_id':2,
						'name':'未通过',
					}
				],
				list_data:{
					state:'',
					name:'',
					limit:10,
					orderby:'inTime desc',
					page:1
				},
				yemxList:[],
				login:false,
				submit_data:{
					user_id:''
				},
				year:date.getFullYear()
			}
		},
		onShow:function(){//返回时接收子页面的传参
			try {
				const intervalID = uni.getStorageSync('intervalID');
				clearInterval(intervalID);
			} catch (e) {
				// error
			}
		},
		onLoad:function(data){
			uni.showLoading({
			  title: '加载中'
			});
			try {//从本地缓存中同步获取指定 key 对应的内容。
				const value = uni.getStorageSync('userinfo');
				if (value) {
					this.list_data.user_id = value['_id'];
					this.submit_data.user_id = value['_id'];
				}
			} catch (e) {
				// error
			}
			this.xid = data.state?parseInt(data.state):'';
			this.list_data.state = this.xid;
			this.getConfig(data);
			this.getList()
		},
		onPullDownRefresh() {
			this.yemxList = [];
			this.page = 0;
			this.limit = 30;
			this.getList()
		},
		onReachBottom() {
			this.getList('add');
		},
		methods: {
			//返回上一页
			goback(){
				uni.navigateBack({
					delta:1
				})
			},
			previewfn(imgs,index){
				this.preview_imgs=imgs;
				this.preview_index = index;
				this.preview=true
			},
			getConfig(index){
				this.$u.api.getOrderIndex(this.submit_data).then(res=>{
					
				})
			},
			yemx(index){
				this.index = [];
				this.page = 0;
				this.limit = 30;
				this.xid =this.oneMenu[index]._id;
				this.list_data.state = this.xid;
				this.getList();
			},
			goOrderDetails(data){//跳转案件详情
				uni.navigateTo({
					url:"/pages/goods_details/order_info?cgd_id="+data.cgd_id+'&type=md'
				})
			},
			getList(action = 'refresh'){
				this.status = 'loading';
				if(action=='add'){
					this.list_data.page = this.list_data.page+1
				}else{
					this.yemxList = [];
					this.list_data.page=1
				}
				//获取信息
				this.$u.api.getPreOrderList(this.list_data).then(res=>{
					uni.stopPullDownRefresh();
					this.xShow = true;
					uni.hideLoading();
					//this.yemxList = res.data.order_info;
					var max_page = res.total/this.limit;
					if(max_page<=this.page){
						this.status = 'nomore';
						return; 
					}
					var datax = []
					if(res.code==1){
						datax = res.data;
					}					
					if (action === 'refresh') {
						this.yemxList = [];
					}	
					
					datax.forEach(item => {		
						let date = new Date(item.inTime*1000);
						item.year = date.getFullYear()					
						this.yemxList.push(item);
					});
					this.page = this.page+1;
					this.status = 'nomore';
				})
			},
			addShouli(index){
				let xinfo = this.yemxList[index];
				this.submit_data._id = xinfo._id;
				uni.showModal({
					title: '提示',
					content: '确认受理此案件',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						if(res.confirm){
							this.$u.api.shouliOrder(this.submit_data).then(res=>{
								uni.showToast({
									icon: 'success',
									title: '受理成功'
								});
								
								if(this.xid===''){
									this.yemxList[index].state=3;
								}else{
									this.yemxList.splice(index, 1);
								}
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			//取消案件
			qxOrder(index){
				let xinfo = this.yemxList[index];
				this.submit_data._id = xinfo._id;
				uni.showModal({
					title: '提示',
					content: '确认取消案件',
					success: res => {
						if (res.confirm) {
							this.$u.api.cancelPreOrder(this.submit_data).then(res=>{
								if(this.xid===''){
									this.yemxList[index].first_trial.id=8;
									this.yemxList[index].first_trial.str='已取消';
								}else{
									this.yemxList.splice(index, 1);
								}
							})
						} else if (res.cancel) {
							//console.log('用户点击取消');
						}						
					}
				});				
			},
			//删除案件
			delOrder(index){
				let xinfo = this.yemxList[index];
				this.submit_data._id = xinfo._id;
				uni.showModal({
					title: '提示',
					content: '确认删除案件',
					success: res => {
						if (res.confirm) {
							this.$u.api.delPreOrder(this.submit_data).then(res=>{
								if(this.xid===''){
									this.yemxList.splice(index, 1);
								}else{
									this.yemxList.splice(index, 1);
								}
							})
						} else if (res.cancel) {
							//console.log('用户点击取消');
						}						
					}
				});				
			},
		}
	}
</script>
<style>
	.active{
		border-bottom:#FF3131 solid 3px;
		color: #FF3131;
	}
</style>
